package cn.outsourcing.supermarket.service.impl;

import cn.outsourcing.supermarket.converter.HiolabsProductConverter;
import cn.outsourcing.supermarket.mapper.HiolabsProductMapper;
import cn.outsourcing.supermarket.po.HiolabsGoods;
import cn.outsourcing.supermarket.po.HiolabsOrderGoods;
import cn.outsourcing.supermarket.po.HiolabsProduct;
import cn.outsourcing.supermarket.service.HiolabsGoodsService;
import cn.outsourcing.supermarket.service.HiolabsOrderGoodsService;
import cn.outsourcing.supermarket.service.HiolabsProductService;
import cn.outsourcing.supermarket.vo.OrderGoodsVO;
import cn.outsourcing.supermarket.vo.hiolabs.HiolabsProductVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务实现类
 *
 * @author gzkemays
 * @since 2021-10-22
 */
@Service
public class HiolabsProductServiceImpl extends ServiceImpl<HiolabsProductMapper, HiolabsProduct>
    implements HiolabsProductService {
  @Resource HiolabsProductMapper productMapper;
  @Resource HiolabsProductService productService;
  @Resource
  HiolabsOrderGoodsService orderGoodsService;

  @Override
  public List<HiolabsProductVO> getProductVoByGoodsId(Integer goodsId) {
    return HiolabsProductConverter.INSTANCE.convertToVoList(
        productMapper.selectList(
            Wrappers.<HiolabsProduct>lambdaQuery().eq(HiolabsProduct::getGoodsId, goodsId)));
  }

  @Override
  public void reduceStock(Integer orderId, Integer number) {
    List<OrderGoodsVO> orderGoods = orderGoodsService.getOrderGoods(orderId);
    List<HiolabsProduct> products = new ArrayList<>();
    for (OrderGoodsVO orderGood : orderGoods) {
      HiolabsProduct product = productMapper.selectById(orderGood.getProductId());
      product.setGoodsNumber(product.getGoodsNumber() - number);
      products.add(product);
    }
    productService.updateBatchById(products);
  }
}
